package com.yanqu.road.server.manager.activity.daomu.pb;

import com.yanqu.road.entity.activity.daomu.pvp.*;
import com.yanqu.road.entity.player.CrossUserBaseInfo;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.pb.activity.DaoMuProto;
import com.yanqu.road.pb.activity.DaoMuPvpProto;
import com.yanqu.road.server.manager.npc.CrossNpcUserMgr;
import com.yanqu.road.server.manager.user.CrossUserMgr;
import com.yanqu.road.utils.string.StringUtils;

import java.util.Map;

public class DaoMuActivityPvpPb {

    public static DaoMuPvpProto.DaoMuPvpPlayerInfo.Builder parseDaoMuPvpPlayerInfo(DaoMuPvpUserData userData) {
        if (userData == null) {
            return null;
        }
        DaoMuPvpProto.DaoMuPvpPlayerInfo.Builder builder = DaoMuPvpProto.DaoMuPvpPlayerInfo.newBuilder();
        builder.setUserId(userData.getUserId());
        builder.setRoomId(userData.getRoomId());
        builder.setPosition(parseDaoMuPvpPlayerPositionData(userData.getPosition()));
        builder.setProperty(parseDaoMuPvpPropertyTemp(userData.getProperty()));
        for (DaoMuPvpBulletData bulletData : userData.getBulletDataList()) {
            builder.addBullet(parseDaoMuPvpBulletTemp(bulletData));
        }
        builder.setOut(userData.isOut());
        builder.setRoleId(userData.getRoleId());
        builder.setCollectionLevel(userData.getCollectionLevel());
        return builder;
    }

    public static DaoMuPvpProto.DaoMuPvpPropertyTemp.Builder parseDaoMuPvpPropertyTemp(DaoMuPvpProperty property) {
        if (property == null) {
            return null;
        }
        DaoMuPvpProto.DaoMuPvpPropertyTemp.Builder builder = DaoMuPvpProto.DaoMuPvpPropertyTemp.newBuilder();
        builder.setLevel(property.getLevel());
        builder.setExp(property.getExp());
        builder.setCurHp(property.getCurHp());
        builder.setReviveCnt(property.getReviveCnt());
        builder.setReviveTime(property.getReviveTime());
        builder.setInvincibleTime(property.getProtectTime());
        builder.setLearnSkillCnt(property.getLearnSkillCnt());
        builder.addAllChooseSkillList(property.getChooseSkillList());
        //属性
        for (Map.Entry<Integer, Long> entry : property.getAttributeMap().entrySet()) {
            DaoMuProto.DaoMuBattleAttribute.Builder temp = DaoMuProto.DaoMuBattleAttribute.newBuilder();
            temp.setId(entry.getKey());
            temp.setValue(entry.getValue());
            builder.addBattleAttributeList(temp);
        }
        // 技能列表
        for (DaoMuPvpSkillData skillData : property.getSkillDataList()) {
            builder.addSkillList(parseDaoMuPvpSkillTemp(skillData));
        }
        // buff列表
        for (DaoMuPvpBuffData buffData : property.getBuffDataList()) {
            DaoMuPvpProto.DaoMuPvpBuffTemp.Builder temp = DaoMuPvpProto.DaoMuPvpBuffTemp.newBuilder();
            temp.setBuffId(buffData.getBuffId());
            temp.setValue(buffData.getValue());
            temp.setExpireTime(buffData.getExpireTime());
            temp.setCreateTime(buffData.getCreateTime());
            builder.addBuffList(temp);
        }
        for (DaoMuPvpSkillData skillData : property.getBattleRecord().getDropSkillList()) {
            builder.addDropSkill(parseDaoMuPvpSkillTemp(skillData));
        }
        if (property.getBattleRecord().getKillUserId() != 0) {
            UserBaseInfo killUser = CrossUserMgr.getUserBaseInfo(property.getBattleRecord().getKillUserId());
            if (killUser != null) {
                builder.setKillUser(PlayerBasePb.parsePlayerBaseTempMsg(killUser));
            }
        }
        if (property.getBattleRecord().getKillMonsterId() != 0) {
            builder.setKillMonsterId(property.getBattleRecord().getKillMonsterId());
        }
        builder.setDropLevel(property.getBattleRecord().getDropLevel());
        return builder;
    }

    private static DaoMuPvpProto.DaoMuPvpSkillTemp.Builder parseDaoMuPvpSkillTemp(DaoMuPvpSkillData skillData) {
        DaoMuPvpProto.DaoMuPvpSkillTemp.Builder temp = DaoMuPvpProto.DaoMuPvpSkillTemp.newBuilder();
        temp.setSkillId(skillData.getSkillId());
        temp.setLevel(skillData.getLevel());
        return temp;
    }

    public static DaoMuPvpProto.DaoMuPvpPlayerPositionData.Builder parseDaoMuPvpPlayerPositionData(DaoMuPvpPosition position) {
        if (position == null) {
            return null;
        }
        DaoMuPvpProto.DaoMuPvpPlayerPositionData.Builder builder = DaoMuPvpProto.DaoMuPvpPlayerPositionData.newBuilder();
        builder.setX((int) Math.floor(position.getX() * 10000));
        builder.setY((int) Math.floor(position.getY() * 10000));
        builder.setDirX((int) Math.floor(position.getDirX() * 10000));
        builder.setDirY((int) Math.floor(position.getDirY() * 10000));
        builder.setTime(position.getTime());
        return builder;
    }

    public static DaoMuPvpProto.DaoMuPvpOtherPlayerInfo.Builder parseDaoMuPvpOtherPlayerInfo(DaoMuPvpUserData otherUserData) {
        if (otherUserData == null) {
            return null;
        }
        DaoMuPvpProto.DaoMuPvpOtherPlayerInfo.Builder builder = DaoMuPvpProto.DaoMuPvpOtherPlayerInfo.newBuilder();
        // 用户基本信息
        CrossUserBaseInfo crossUserBaseInfo = CrossUserMgr.getCrossUserBaseInfo(otherUserData.getUserId());
        builder.setUserId(otherUserData.getUserId());
        if (crossUserBaseInfo == null) {
            // 找一下npc
            crossUserBaseInfo = CrossNpcUserMgr.getCrossNpcUserBaseInfo(otherUserData.getUserId());
        }
        if (crossUserBaseInfo != null) {
            builder.setNickName(crossUserBaseInfo.getUserBaseInfo().getNickName());
        }
        // 属性
        builder.setProperty(parseDaoMuPvpPropertyTemp(otherUserData.getProperty()));
        builder.setPosition(parseDaoMuPvpPlayerPositionData(otherUserData.getPosition()));
        for (DaoMuPvpBulletData bulletData : otherUserData.getBulletDataList()) {
            builder.addBullet(parseDaoMuPvpBulletTemp(bulletData));
        }
        builder.setRoleId(otherUserData.getRoleId());
        builder.setCollectionLevel(otherUserData.getCollectionLevel());
        return builder;
    }


    public static DaoMuPvpProto.DaoMuPvpBulletTemp.Builder parseDaoMuPvpBulletTemp(DaoMuPvpBulletData bulletData) {
        if (bulletData == null) {
            return null;
        }
        DaoMuPvpProto.DaoMuPvpBulletTemp.Builder builder = DaoMuPvpProto.DaoMuPvpBulletTemp.newBuilder();
        builder.setUserId(bulletData.getUserId());
        builder.setBulletId(bulletData.getBulletId());
        builder.setCreateTime(bulletData.getCreateTime());
        builder.setAngle(String.valueOf(bulletData.getInitialAngle()));
        return builder;
    }

    public static DaoMuPvpPosition parseDaoMuActivityPvpPosition(DaoMuPvpProto.DaoMuPvpPlayerPositionData positionMsg) {
        if (positionMsg == null) {
            return null;
        }
        DaoMuPvpPosition position = new DaoMuPvpPosition();
        position.setX(positionMsg.getX() / 10000.0);
        position.setY(positionMsg.getY() / 10000.0);
        position.setDirX(positionMsg.getDirX() / 10000.0);
        position.setDirY(positionMsg.getDirY() / 10000.0);
        position.setTime(positionMsg.getTime());
        return position;
    }

    public static DaoMuPvpProto.DaoMuPvpCoordinateTemp.Builder parseDaoMuPvpCoordinateTemp(Coordinate coordinate) {
        if (coordinate == null) {
            return null;
        }
        DaoMuPvpProto.DaoMuPvpCoordinateTemp.Builder builder = DaoMuPvpProto.DaoMuPvpCoordinateTemp.newBuilder();
        builder.setX((int) Math.floor(coordinate.getX() * 10000));
        builder.setY((int) Math.floor(coordinate.getY() * 10000));
        return builder;
    }

    public static DaoMuPvpProto.DaoMuPvpRoomItemDataTemp.Builder parseDaoMuPvpRoomItemDataTemp(DaoMuPvpBoxData boxData) {
        if (boxData == null) {
            return null;
        }
        DaoMuPvpProto.DaoMuPvpRoomItemDataTemp.Builder builder = DaoMuPvpProto.DaoMuPvpRoomItemDataTemp.newBuilder();
        builder.setId(boxData.getUid());
        builder.setType(1);
        builder.setCoordinate(parseDaoMuPvpCoordinateTemp(boxData.getCoordinate()));
        builder.setAttackCnt(boxData.getAttackCnt());
        return builder;
    }
    public static DaoMuPvpProto.DaoMuPvpRoomItemDataTemp.Builder parseDaoMuPvpRoomItemDataTemp(DaoMuPvpGoodsData goodsData) {
        if (goodsData == null) {
            return null;
        }
        DaoMuPvpProto.DaoMuPvpRoomItemDataTemp.Builder builder = DaoMuPvpProto.DaoMuPvpRoomItemDataTemp.newBuilder();
        builder.setId(goodsData.getUid());
        builder.setType(2);
        builder.setCoordinate(parseDaoMuPvpCoordinateTemp(goodsData.getCoordinate()));
        builder.setGoodsId(goodsData.getGoodsId());
        if (!StringUtils.isNullOrEmpty(goodsData.getExtraData())) {
            builder.setCount(Integer.parseInt(goodsData.getExtraData()));
        }
        return builder;
    }

    public static DaoMuPvpProto.DaoMuPvpMonsterTemp.Builder parseDaoMuPvpMonsterTemp(DaoMuPvpMonsterData monsterData) {
        if (monsterData == null) {
            return null;
        }
        DaoMuPvpProto.DaoMuPvpMonsterTemp.Builder builder = DaoMuPvpProto.DaoMuPvpMonsterTemp.newBuilder();
        builder.setMonsterId(monsterData.getMonsterId());
        builder.setFaceId(monsterData.getFaceId());
        builder.setSpeed(monsterData.getSpeed());
        builder.setPosition(parseDaoMuPvpPlayerPositionData(monsterData.getPosition()));
        builder.setAttackCnt(monsterData.getAttackCnt());
        return builder;
    }

    public static DaoMuPvpProto.DaoMuPvpNoticeTemp.Builder parseDaoMuPvpNoticeTemp(DaoMuPvpNoticeData noticeData) {
        DaoMuPvpProto.DaoMuPvpNoticeTemp.Builder builder = DaoMuPvpProto.DaoMuPvpNoticeTemp.newBuilder();
        if (noticeData.getUser1() != null) {
            builder.setUser1(parseDaoMuPvpNoticeUserTemp(noticeData.getUser1()));
        }
        if (noticeData.getUser2() != null) {
            builder.setUser2(parseDaoMuPvpNoticeUserTemp(noticeData.getUser2()));
        }
        builder.setType(noticeData.getType());
        builder.setContent(noticeData.getContent());
        builder.setTime(noticeData.getTime());
        return builder;
    }

    public static DaoMuPvpProto.DaoMuPvpNoticeUserTemp.Builder parseDaoMuPvpNoticeUserTemp(DaoMuPvpNoticeUserData noticeUserData) {
        DaoMuPvpProto.DaoMuPvpNoticeUserTemp.Builder builder = DaoMuPvpProto.DaoMuPvpNoticeUserTemp.newBuilder();
        builder.setServerId(noticeUserData.getServerId());
        builder.setNickName(noticeUserData.getNickName());
        builder.setDecoration(noticeUserData.getDecoration());
        return builder;
    }
}
